// globalData---》全局树形数据，存放增加，删除，修改后的树形数据
var globalData = []
    // SQstatus---》收起状态标记值
var SQstatus = false
    // ZKstatus---》展开状态标记值
var ZKstatus = false
    // currentGlobalData---》当前编辑缓存
var currentGlobalData = {}
    // currentGlobalData---》当前新增缓存
    var currentGlobalData1 = {}
    /* 
      formatData 函数：数据转换，将一维数组数据转换为树形数据
      参数：data---》原始一维数组数据
      返回值：resultArr---》格式化好的树形数据
    */
function formatData(data, pid) {
    var resultArr = [], temp;
    for (var i = 0; i < data.length; i++) {
        if (data[i].menuParentid == pid) {
            var obj = JSON.parse(JSON.stringify(data[i]));
            temp = formatData(data, data[i].menuId);
            if (temp.length > 0) {
                obj.children = temp;
            }
            resultArr.push(obj);
        }
    }
    return resultArr;
}
/* 
  initTable 函数: 初始化表格
  参数: data---》转换的树形数据
*/
function initTable(data) {
    globalData = JSON.parse(JSON.stringify(data))
    var tableEL = document.createElement('table')
    var containerEL = document.getElementById('container')
    containerEL.appendChild(tableEL)
    tableEL.appendChild(createTH())
    initTR_DG(data,tableEL,'')
}
function initTR_DG(data,tableEL,index){
    if(data && data.length != 0){
        for(var i=0; i<data.length; i++){
            if(data[i].menuType == '菜单') {
                var trEL = createTR(data[i], 1, index+'.'+(i+1))
            } else {
                var trEL = createTR(data[i], 0, index+'.'+(i+1))
            }
            tableEL.appendChild(trEL)
            if(data[i].children && data[i].children.length != 0){
                initTR_DG(data[i].children,tableEL,index+'.'+(i+1))
            }
        }
    }
}
/*
  createTH 函数: 创建表头
  返回值: trEL---》表头元素
*/
function createTH() {
    var trEL = document.createElement('tr')
    var thEL0 = document.createElement('th')
    thEL0.innerHTML = '序号'
    trEL.appendChild(thEL0)
    var thEL1 = document.createElement('th')
    thEL1.innerHTML = '功能名称'
    trEL.appendChild(thEL1)
    var thEL2 = document.createElement('th')
    thEL2.innerHTML = '是否启用'
    trEL.appendChild(thEL2)
    var thEL3 = document.createElement('th')
    thEL3.innerHTML = '功能类型'
    trEL.appendChild(thEL3)
    var thEL4 = document.createElement('th')
    thEL4.innerHTML = '创建时间'
    trEL.appendChild(thEL4)
    var thEL5 = document.createElement('th')
    thEL5.innerHTML = '功能备注'
    trEL.appendChild(thEL5)
    var thEL6 = document.createElement('th')
    thEL6.innerHTML = '相关操作'
    trEL.appendChild(thEL6)
    return trEL
}
/* 
  createTR 函数: 创建每一列
  参数1: currentData---》当前列的数据
  参数2: type---》类型，决定有没有展开折叠按钮，1 有，其他无
  参数3 index---》计算的当前列的标号
  参数4: tier---》第几层
  返回值: trEL---》创建的当前 tr
*/
function createTR(currentData, type, index) {
    var trEL = document.createElement('tr')
    if (type == 1) {
        var tdEL0 = document.createElement('td')
        tdEL0.innerHTML = index
        tdEL0.style.fontWeight = 'bold'
        tdEL0.style.fontSize = '8px'
        trEL.appendChild(tdEL0)
        var spanEL = document.createElement('span')
        spanEL.classList.add('span')
        spanEL.title = '展开'
        spanEL.onclick = clickZK
        var spanEL1 = document.createElement('span')
        spanEL1.classList.add('span1')
        spanEL1.title = '收起'
        spanEL1.onclick = clickSQ
        var tdEL1 = document.createElement('td')
        tdEL1.innerHTML = currentData.menuText
        tdEL0.style.textAlign = 'left'
        tdEL0.appendChild(spanEL)
        tdEL0.appendChild(spanEL1)
        trEL.appendChild(tdEL1)
        var tdEL2 = document.createElement('td')
        tdEL2.innerHTML = currentData.menuIsmodify
        trEL.appendChild(tdEL2)
        var tdEL3 = document.createElement('td')
        tdEL3.innerHTML = currentData.menuType
        currentData.menuType == '菜单' ? tdEL3.style.color = '#409EFF' : tdEL3.style.color = 'green'
        trEL.appendChild(tdEL3)
        var tdEL4 = document.createElement('td')
        tdEL4.innerHTML = datefomate(currentData.menuCreatetime)
        trEL.appendChild(tdEL4)
        var tdEL5 = document.createElement('td')
        tdEL5.innerHTML = currentData.menuNote
        trEL.appendChild(tdEL5)
        var tdEL6 = document.createElement('td')
        var editBtn = document.createElement('button')
        editBtn.innerText = '编辑'
        editBtn.classList.add('button1')
        editBtn.onclick = editTableData
        tdEL6.appendChild(editBtn)
        var delBtn = document.createElement('button')
        delBtn.innerText = '删除'
        delBtn.classList.add('button2')
        delBtn.onclick = delTableData
        tdEL6.appendChild(delBtn)
        var addBtn = document.createElement('button')
        addBtn.innerText = '新增'
        addBtn.onclick = addTableData
        tdEL6.appendChild(addBtn)
        addBtn.classList.add('button0')
        trEL.appendChild(tdEL6)
    } else {
        var tdEL0 = document.createElement('td')
        var strongEL = document.createElement('strong')
        strongEL.innerHTML = index
        tdEL0.appendChild(strongEL)
        tdEL0.style.fontSize = '8px'
        trEL.appendChild(tdEL0)
        var tdEL1 = document.createElement('td')
        tdEL1.innerHTML = currentData.menuText
        trEL.appendChild(tdEL1)
        var tdEL2 = document.createElement('td')
        tdEL2.innerHTML = currentData.menuIsmodify
        trEL.appendChild(tdEL2)
        var tdEL3 = document.createElement('td')
        tdEL3.innerHTML = currentData.menuType
        currentData.menuType == '菜单' ? tdEL3.style.color = '#409EFF' : tdEL3.style.color = 'green'
        trEL.appendChild(tdEL3)
        var tdEL4 = document.createElement('td')
        tdEL4.innerHTML = datefomate(currentData.menuCreatetime)
        trEL.appendChild(tdEL4)
        var tdEL5 = document.createElement('td')
        tdEL5.innerHTML = currentData.menuNote
        trEL.appendChild(tdEL5)
        var tdEL6 = document.createElement('td')
        var editBtn = document.createElement('button')
        editBtn.innerText = '编辑'
        editBtn.classList.add('button1')
        editBtn.onclick = editTableData
        tdEL6.appendChild(editBtn)
        var delBtn = document.createElement('button')
        delBtn.innerText = '删除'
        delBtn.classList.add('button2')
        delBtn.onclick = delTableData
        tdEL6.appendChild(delBtn)
        trEL.appendChild(tdEL6)
    }
    return trEL
}
// clickSQ 函数: 列收起
function clickSQ() {
    initData = JSON.parse(JSON.stringify(globalData))
    var currentText = this.parentNode.parentNode.children[1].innerText
    clickSQ_DG(globalData,currentText)
    document.getElementById('container').innerHTML = ''
    initTable(globalData)
    SQstatus = false
}
function clickSQ_DG(globalData,currentText){
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            globalData[i].children = []
            SQstatus = true
            break
        }
        if(SQstatus == false){
            if(globalData[i].children && globalData[i].children.length != 0){
                clickSQ_DG(globalData[i].children,currentText)
            }
        }
    }
}
// clickZK 函数: 列展开
function clickZK() {
    document.getElementById('container').innerHTML = ''
    initTable(initData)
}
// delTableData函数: 删除操作
function delTableData() {
    var currentText = this.parentNode.parentNode.children[1].innerText
    delTableData_DG(globalData,currentText)
    document.getElementById('container').innerHTML = ''
    initTable(globalData)
    SQstatus = false
}
function delTableData_DG(globalData,currentText){
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            globalData.splice(i, 1)
            SQstatus = true
            break
        }
        if(SQstatus == false){
            if(globalData[i].children && globalData[i].children.length != 0){
                delTableData_DG(globalData[i].children,currentText)
            }
        }
    }
}
// editTableData 函数: 编辑操作
function editTableData() {
    document.getElementById('edit').style.display = 'block'
    var currentText = this.parentNode.parentNode.children[1].innerText
    editTableData_DG(globalData,currentText)
    SQstatus = false
}
function editTableData_DG(globalData,currentText){
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            setEditData(globalData[i])
            getCurrentEdit(globalData[i])
            SQstatus = true
            return
        }
        if(SQstatus == false){
            if(globalData[i].children && globalData[i].children.length != 0){
                editTableData_DG(globalData[i].children,currentText,currentGlobalData)
            }
        }
    }
}
function getCurrentEdit(data){
    return currentGlobalData = data
}
/* 
  etEditData 函数: 编辑数据回显
  参数: data---》编辑回显的当前数据
*/
function setEditData(data) {
    document.getElementById('menuText').value = data.menuText
    document.getElementById('menuIsmodify').value = data.menuIsmodify == '启用' ? 1 : 0
    document.getElementById('menuType').value = data.menuType == '菜单' ? 1 : 0
    document.getElementById('menuCreatetime').value = datefomate(data.menuCreatetime)
    document.getElementById('menuNote').value = data.menuNote
}
// 编辑关闭
document.getElementById('editClose').onclick = function() {
        document.getElementById('edit').style.display = 'none'
    }
    // 编辑保存
document.getElementById('editSave').onclick = function() {
        currentGlobalData.menuText = document.getElementById('menuText').value
        currentGlobalData.menuIsmodify = document.getElementById('menuIsmodify').value == 1 ? '启用' : '禁用'
        currentGlobalData.menuType = document.getElementById('menuType').value == 1 ? '菜单' : '链接'
        currentGlobalData.menuCreatetime = document.getElementById('menuCreatetime').value
        currentGlobalData.menuNote = document.getElementById('menuNote').value
        document.getElementById('container').innerHTML = ''
        initTable(globalData)
        document.getElementById('edit').style.display = 'none'
    }
    // 编辑取消
document.getElementById('editCancle').onclick = function() {
        document.getElementById('edit').style.display = 'none'
    }
    // addTableData 函数：新增操作
function addTableData() {
    document.getElementById('add').style.display = 'block'
    document.getElementById('menuCreatetime1').value = datefomate(Date.now())
    var currentText = this.parentNode.parentNode.children[1].innerText
    addTableData_DG(globalData,currentText)
    SQstatus = false
}
function addTableData_DG(globalData,currentText){
    for (var i = 0; i < globalData.length; i++) {
        if (globalData[i].menuText == currentText) {
            getCurrentAdd(globalData[i])
            SQstatus = true
            return
        }
        if(SQstatus == false){
            if(globalData[i].children && globalData[i].children.length != 0){
                addTableData_DG(globalData[i].children,currentText)
            }
        }
    }
}
function getCurrentAdd(data){
    return currentGlobalData1 = data
}
// 新增关闭
document.getElementById('addClose').onclick = function() {
        document.getElementById('menuText1').value = ''
        document.getElementById('menuNote1').value = ''
        document.getElementById('add').style.display = 'none'
    }
    //新增保存
document.getElementById('addSave').onclick = function() {
        var obj = {}
        obj.children = []
        obj.menuText = document.getElementById('menuText1').value
        obj.menuIsmodify = document.getElementById('menuIsmodify1').value == 1 ? '启用' : '禁用'
        obj.menuType = document.getElementById('menuType1').value == 1 ? '菜单' : '链接'
        obj.menuCreatetime = document.getElementById('menuCreatetime1').value
        obj.menuNote = document.getElementById('menuNote1').value
        if(!currentGlobalData1.children){
            currentGlobalData1.children = []
            currentGlobalData1.children[0] = obj
        }else{
            currentGlobalData1.children.push(obj)
        }
        document.getElementById('container').innerHTML = ''
        initTable(globalData)
        document.getElementById('menuText1').value = ''
        document.getElementById('menuNote1').value = ''
        document.getElementById('add').style.display = 'none'
    }
    // 新增取消
document.getElementById('addCancle').onclick = function() {
        document.getElementById('menuText1').value = ''
        document.getElementById('menuNote1').value = ''
        document.getElementById('add').style.display = 'none'
    }
    // datefomate函数: 格式化日期
function datefomate(value) {
    if (value == null || value == undefined) {
        return "";
    }
    var date = new Date(value);
    Y = date.getFullYear(),
    m = date.getMonth() + 1,
    d = date.getDate(),
    H = date.getHours(),
    i = date.getMinutes(),
    s = date.getSeconds();
    return Y + '-' + m + '-' + d + '    ' + H + ':' + i + ':' + s;
};